package com.turing.post.organization.impl;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.ConsumeTerminal;
import com.turing.post.organization.util.ConsumeTerminalBusiness;
import com.turing.post.organization.util.ConsumeTerminalDao;
import com.turing.post.thirdinterface.util.ThirdInterfaceApplicationException;
import com.turing.post.thirdinterface.util.ThirdInterfaceRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 商户脱机消费终端管理Business实现
 * 
 * @author kangzhe
 * 
 */
@Stateless(mappedName = "organization.impl.ConsumeTerminalBusinessImpl")
@Remote
@Local
public class ConsumeTerminalBusinessImpl implements ConsumeTerminalBusiness {

	@EJB(mappedName = "organization.impl.ConsumeTerminalDaoImpl")
	private ConsumeTerminalDao ctDao;

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	/**
	 * 异常抛出方法
	 * 
	 * @param error
	 * @throws PCPSApplicationException
	 */
	private void errException(String error) throws PCPSApplicationException {
		throw new ThirdInterfaceApplicationException(
				ThirdInterfaceApplicationException.ERRCODE_CATEGORY_APPLICATION,
				ThirdInterfaceRespCode.CONSUMETERMINAL_INSERT_UPDATE, error);
	}

	@Override
	public void insertConsumeTerminal(PCPSWebInput webInput, ConsumeTerminal ct)
			throws PCPSApplicationException {
		/**
		 * 参数合法性检查
		 */
		if (webInput.getOperId() == null) {
			errException("登录用户为空!");
		}
		if (ct == null) {
			errException("商户脱机消费终端信息传输为空!");
		}
		if (PCPSUtil.isEmpty(ct.getTerminalNo())) {
			errException("读卡器号为空!");
		}
		if (ctDao.getConsumeTerminalByTerminalNo(ct.getTerminalNo()) != null) {
			errException("商户脱机消费终端信息已存在!");
		}
		if (PCPSUtil.isEmpty(ct.getMerchantNo())) {
			errException("商户号为空!");
		}
		if (PCPSUtil.isEmpty(ct.getMerchantBranchNo())) {
			errException("商户机构号为空!");
		}
		if (PCPSUtil.isEmpty(ct.getPsamCardNo())) {
			errException("PSAM卡号为空!");
		}
		if (PCPSUtil.isEmpty(ct.getPsamSeqNo())) {
			errException("PSAM卡序号为空!");
		}
		if (PCPSUtil.isEmpty(ct.getPsamKey())) {
			errException("PSAM卡密钥为空!");
		}
		/**
		 * 新增商户脱机消费终端信息
		 */
		ct.setOpenTime(commonDaoImpl.getDatabaseSysTime());
		ctDao.insert(ct);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "新增商户脱机消费终端", "新增商户脱机消费终端， 终端号"
						+ ct.getTerminalNo());
	}

	@Override
	public void updateConsumeTerminal(PCPSWebInput webInput, ConsumeTerminal ct)
			throws PCPSApplicationException {
		/**
		 * 参数合法性检查
		 */
		if (webInput.getOperId() == null) {
			errException("登录用户为空!");
		}
		if (ct == null) {
			errException("商户脱机消费终端信息传输为空!");
		}
		if (PCPSUtil.isEmpty(ct.getTerminalNo())) {
			errException("读卡器号为空!");
		}
		ConsumeTerminal ctobj = ctDao.getConsumeTerminalByTerminalNo(ct
				.getTerminalNo());
		if (ctobj == null) {
			errException("商户脱机消费终端信息不存在!");
		}
		if (PCPSUtil.isEmpty(ct.getMerchantNo())) {
			errException("商户号为空!");
		}
		if (PCPSUtil.isEmpty(ct.getMerchantBranchNo())) {
			errException("商户机构号为空!");
		}
		if (PCPSUtil.isEmpty(ct.getPsamCardNo())) {
			errException("PSAM卡号为空!");
		}
		if (PCPSUtil.isEmpty(ct.getPsamSeqNo())) {
			errException("PSAM卡序号为空!");
		}
		if (PCPSUtil.isEmpty(ct.getPsamKey())) {
			errException("PSAM卡密钥为空!");
		}
		/**
		 * 修改商户脱机消费终端信息
		 */
		ctobj.setMerchantNo(ct.getMerchantNo());
		ctobj.setMerchantBranchNo(ct.getMerchantBranchNo());
		ctobj.setTerminalNo(ct.getTerminalNo());
		ctobj.setPsamCardNo(ct.getPsamCardNo());
		ctobj.setPsamSeqNo(ct.getPsamSeqNo());
		ctobj.setPsamKey(ct.getPsamKey());
		ctobj.setRemark(ct.getRemark());
		ctDao.update(ctobj);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "修改商户脱机消费终端", "修改商户脱机消费终端， 终端号"
						+ ct.getTerminalNo());
	}

}
